package com.bupt.basic;

import com.bupt.exception.BaseException;
import com.bupt.utils.LocalVerify;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 链路上的信息
 *
 * @author breakpoint/赵先生
 * 2020/11/29
 */
@Data
public final class ServiceMsgOnLink implements Serializable {

    private static final long serialVersionUID = 8719910557281934735L;

    private List<ServiceMsg> serviceMsgs = new ArrayList<ServiceMsg>();

    public void clear() throws BaseException {
        if (null != this.serviceMsgs && this.serviceMsgs.size() > 0) {
            this.serviceMsgs.clear();
        }
    }

    // 移除的操作
    public void removeServiceMsg(String serviceId) throws BaseException {
        LocalVerify.verifyString(serviceId, "业务ID");
        if (null != this.serviceMsgs && this.serviceMsgs.size() > 0) {
            Iterator<ServiceMsg> iterator = this.serviceMsgs.iterator();
            while (iterator.hasNext()) {
                ServiceMsg next = iterator.next();
                if (next.serviceId.equals(serviceId)) {
                    iterator.remove();
                    break;
                }
            }
        }
    }

    // 添加一条信息
    public void addServiceMsg(String pId, String pName, String demandUserName, DemandReportService service) throws BaseException {
        LocalVerify.verifyString(pId, "项目ID");
        LocalVerify.verifyString(pName, "项目名字");
        LocalVerify.verifyObject(service, "service对象");
        if (null == this.serviceMsgs) this.serviceMsgs = new ArrayList<ServiceMsg>();
        this.serviceMsgs.add(new ServiceMsg(pId, pName, service.getId(), service.getName(), service.getType(),
                service.getBandwidthNum(), demandUserName));
    }

    @Data
    public static final class ServiceMsg implements Serializable {

        private static final long serialVersionUID = -4231469036555711871L;

        // 项目的ID
        private String pId;

        // 项目的名字
        private String pName;

        // 业务的ID
        private String serviceId;

        // 业务的名字
        private String serviceName;

        // 业务的类型
        private String serviceType;

        // 业务的带宽
        private Double serviceRate;

        // 提交人 # issue 设置提交的人 方便与查看操作
        private String demandUserName;

        public ServiceMsg(String pId, String pName, String serviceId, String serviceName,
                          String serviceType, Double serviceRate, String demandUserName) {
            this.pId = pId;
            this.pName = pName;
            this.serviceId = serviceId;
            this.serviceName = serviceName;
            this.serviceType = serviceType;
            this.serviceRate = serviceRate;
            this.demandUserName = demandUserName;
        }
    }
}
